import Vue from "vue"
import VueRouter from "vue-router"

import Home from "../views/Home.vue"

Vue.use(VueRouter)

const routes = [
  // 主页
  {
    path: "/home",
    name: "Home",
    component: Home,
    meta: {
      keepAlive: true, // 需要缓存
    },
  },
  //自己调试的页面火箭的
  {
    path: "/huojian",
    name: "Huojian",
    component: () => import("../views/Sss"),
  },
  //评论的页面
  {
    path: "/my_comments",
    name: "My_comments",
    component: () => import("../views/my_comments/my_comments.vue"),
  },
  //评价完成页面
  {
    path: "/your_comments",
    name: "your_comments",
    component: () => import("../views/Your_comments/Your_comments.vue"),
  },
  //人工客服的制作
  {
    path: "/customer",
    name: "Customer",
    component: () => import("../views/Login/Test3.vue"),
  },

  // 商品的搜索页
  {
    path: "/search",
    name: "Search",
    component: () => import("../views/Search/Search"),
  },
  //商品的搜索报错页面
  {
    path: "/cant_find",
    name: "Cant_find",
    component: () => import("../views/Cant_find/Cant_find.vue"),
  },
  //商品查看的评论页
  {
    path: "/comment",
    name: "Comment",
    component: () => import("../views/Comment/Comment"),
  },
  
  // vip轮播图
  {
    path: "/display",
    name: "Display",
    component: () => import("../views/Display/Display"),
  },
  //商品的VIP
  {
    path: "/vip",
    name: "Vip",
    component: () => import("../views/Vip.vue"),
  },
  //商品的详情页
  {
    path: "/detail",
    name: "Detail",
    component: () => import("../views/Detail/Detail.vue"),
    meta: {
      keepAlive: false, // 需要缓存
    },
  },
  //商品列表
  {
    path: "/product",
    name: "Product",
    component: () => import("../views/Product/Product.vue"),
  },
  //登陆进入的广告页面
  {
    path: "/",
    name: "Advert",
    component: () => import("../views/Advert.vue"),
  },
  // 订单
  {
    path: "/order",
    name: "Order",
    component: () => import("../views/Card/Order.vue"),
  },
  // 购物车
  {
    path: "/card",
    name: "Card",
    component: () => import("../views/Card/Card.vue"),
  },
  // 购物车跳转的订单详情
  {
    path: "/orderDetail",
    name: "OrderDetail",
    component: () => import("../views/UserCenter/OrderDetail.vue"),
  },
  // 用户直接购买的订单页面
  {
    path: '/buy',
    name: 'Buy',
    component: () => import('../views/Card/Buy.vue'),
  },
  // {
  //   path: "/login",
  //   name: "Login",
  //   component: () => import("../views/Login/Login.vue"),
  // },
  // 用户中心
  {
    path: "/usercenter",
    name: "UserCenter",
    component: () => import("../views/UserCenter/UserCenter.vue"),
    children: [
      {
        path: "login",
        name: "Login",
        component: function () {
          return import(
            /* webpackChunkName: "about" */ "../views/Login/Login.vue"
          )
        },
      },
    ],
  },
  {
    path: "/material",
    name: "Material",
    component: () => import("../views/UserCenter/Material.vue"),
  },
  // 登录
  // {
  //   path: "/login",
  //   name: "Login",
  //   component: () => import("../views/Login/Login.vue"),
  // },
  // 联系人地址
  {
    path: "/address",
    name: "Address",
    component: () => import("../views/Address.vue"),
  },

  {
    path: "/turntable",
    component: () => import("../components/Turntable/Turntable.vue"),
  },
  {
    path: "/integral",
    name: "Integral",
    component: () => import("../views/Integral/Integral.vue"),
  },
  {
    path: "/coupon",
    name: "Coupon",
    component: () => import("../components/Coupon/Coupon.vue"),
  },
  {
    path: "/coupon1",
    name: "Coupon1",
    component: () => import("../components/Coupon/Coupon1.vue"),
  },
  {
    path: "/test",
    name: "Test",
    component: () => import("../views/Login/Test.vue"),
  },
  {
    path: "/confirm",
    name: "Confirm",
    component: () => import("../views/Card/Confirm.vue"),
  },
]
const router = new VueRouter({
  // mode: "history",
  base: process.env.BASE_URL,
  routes,
})

// 路由守卫  to  前往路由   from  来源路由   next是否下一步
router.beforeEach((to, from, next) => {
  // 积分需要先登录才能查看
  if (to.name == "Integral" && localStorage.getItem("token") == null) {
    if (from.name != "UserCenter") {
      next("/usercenter")
    }
    return
  }
  // 优惠券需要先登录才能查看
  if (to.name == "Coupon" && localStorage.getItem("token") == null) {
    if (from.name != "UserCenter") {
      next("/usercenter")
    }
    return
  }
  next()
})

export default router
